#include "Chromosome.h"
#include "parameters.h"

bool Chromosome::minimize = false;

Chromosome::Chromosome() : fitness(0.0)
{
    TRACE_FUNC
}

Chromosome::Chromosome(const Chromosome &other)
{
    TRACE_FUNC
    fitness = other.fitness;
}

Chromosome::~Chromosome()
{
}

Chromosome& Chromosome::operator=(const Chromosome& other)
{
    TRACE_FUNC_ENTRY

    if (&other == this)
        return *this;

    fitness = other.fitness;

    TRACE_FUNC_EXIT
    return *this;
}

void Chromosome::setFitness(double _fitness)
{
    TRACE_FUNC
    fitness = _fitness;
}

double Chromosome::getFitness() const
{
    return fitness;
}

void Chromosome::mutate()
{
    int mutationPosition;
    //mutation position from 0 to chromosome size-1
    mutationPosition = rand() % size();
    mutate(mutationPosition);
}

void Chromosome::additionalOperators(int /*position*/)
{
    TRACE_FUNC
    return;
}

void Chromosome::additionalOperators(const Chromosome&)
{
    TRACE_FUNC
    return;
}

bool Chromosome::operator<(const Chromosome& other) const
{
    TRACE_FUNC
    return (this->fitness<other.fitness) ^ minimize;
}

bool Chromosome::greaterThan(const Chromosome* chr1, const Chromosome* chr2)
{
    //TRACE_FUNC
    return (chr2->fitness < chr1->fitness) ^ minimize;
}

QDebug operator<<(QDebug dbg, const Chromosome& chr)
{
    dbg.nospace() << chr.toString();
    dbg.space() << " fitness: " << QString::number(chr.getFitness(),'g',20) << " length: " << chr.size();
    return dbg.maybeSpace();
}
